package cc.vv.baselibrary.view;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Color;
import android.support.annotation.Nullable;
import android.text.TextPaint;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import cc.vv.baselibrary.R;
import cc.vv.lklibrary.anno.NPFInject;
import cc.vv.lklibrary.log.LogOperate;


/**
 * 公共的顶部栏TopBar
 *
 * @author dup dupeng@vv.cc
 * @date 2018/3/13
 */

public class BaseTopBarView extends RelativeLayout {

    /**
     * 上下文
     */
    private Context mContext;

    /**
     * 父容器
     */
    private RelativeLayout rl_ljtb_layout;

    /**
     * 布局左侧按钮容器
     */
    private LinearLayout rl_ljtb_left_layout;
    /**
     * 布局左侧关闭按钮容器
     */
    private LinearLayout rl_ljtb_left_close_layout;

    /**
     * 布局左侧图标控件
     */
    private ImageView iv_ljtb_left_back;

    /**
     * 布局左侧文本控件
     */
    private TextView tv_ljtb_left_back;

    /**
     * 布局中间标题文本控件
     */
    private TextView tv_ljtb_title;

    /**
     * 布局右侧按钮容器
     */
    private LinearLayout rl_ljtb_right_layout;

    /**
     * 布局右侧图标控件
     */
    private ImageView iv_ljtb_right_back;

    /**
     * 布局右侧文本控件
     */
    private TextView tv_ljtb_right_back;

    /**
     * 布局底部分割线控件
     */
    private View view_ljtb_divider;

    /**
     * 布局控件事件接口对象
     */
    private EventInterface eventInterface;

    public BaseTopBarView(Context context) {
        this(context, null);
    }

    public BaseTopBarView(Context context, @Nullable AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public BaseTopBarView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        mContext = context;
        initView();
        initViewEvent();
        handleAttrs(attrs, defStyleAttr);
    }

    /**
     * 初始化布局控件
     */
    private void initView() {
        if (mContext == null) {
            return;
        }
        View view = LayoutInflater.from(mContext).inflate(R.layout.view_top_bar, null);
        NPFInject.getInstance().inject(this, view);
        rl_ljtb_layout = view.findViewById(R.id.rl_vtb_layout);
        tv_ljtb_title = view.findViewById(R.id.tv_vtb_title);
        rl_ljtb_left_layout = view.findViewById(R.id.rl_vtb_left_layout);
        rl_ljtb_left_close_layout = view.findViewById(R.id.rl_vtb_left_close_layout);
        iv_ljtb_left_back = view.findViewById(R.id.iv_vtb_left_back);
        tv_ljtb_left_back = view.findViewById(R.id.tv_vtb_left_back);
        rl_ljtb_right_layout = view.findViewById(R.id.rl_vtb_right_layout);
        iv_ljtb_right_back = view.findViewById(R.id.iv_vtb_right_back);
        tv_ljtb_right_back = view.findViewById(R.id.tv_vtb_right_back);
        view_ljtb_divider = view.findViewById(R.id.view_vtb_divider);
        this.addView(view);
    }

    /**
     * 初始化布局控件事件
     */
    private void initViewEvent() {
        if (mContext == null) {
            return;
        }
        if (eventInterface != null) {
            if (rl_ljtb_left_layout != null) {
                if (iv_ljtb_left_back.getVisibility() == View.VISIBLE || tv_ljtb_left_back.getVisibility() == View.VISIBLE) {
                    rl_ljtb_left_layout.setOnClickListener(new OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            eventInterface.leftOnClick();
                        }
                    });
                }
            }

            if (rl_ljtb_left_close_layout != null) {
                rl_ljtb_left_close_layout.setOnClickListener(new OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        eventInterface.leftCloseClick();
                    }
                });
            }

            if (rl_ljtb_right_layout != null) {
                if (iv_ljtb_right_back.getVisibility() == View.VISIBLE || tv_ljtb_right_back.getVisibility() == View.VISIBLE) {
                    rl_ljtb_right_layout.setOnClickListener(new OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            eventInterface.rightOnClick();
                        }
                    });
                }
            }
        }
    }

    private void handleAttrs(AttributeSet attrs, int defStyleAttr) {
        if (mContext == null) {
            return;
        }
        TypedArray typedArray;
        try {
            typedArray = mContext.obtainStyledAttributes(attrs, R.styleable.BaseTopBarView, defStyleAttr, 0);
        } catch (Exception e) {
            LogOperate.e(e.getMessage());
            typedArray = null;
        }
        if (typedArray != null) {
            setLayoutBGColor(typedArray.getColor(R.styleable.BaseTopBarView_layoutBGColor, Color.parseColor("#FF693C26")));

//            setLeftBtnImage(typedArray.getResourceId(R.styleable.BaseTopBarView_leftIVResources, R.drawable.view_top_bar_selector_back_bg));
            setLeftBtnImage(typedArray.getResourceId(R.styleable.BaseTopBarView_leftIVResources, R.mipmap.icon_ctm_cannel));
            setLeftBtnImageShow(typedArray.getBoolean(R.styleable.BaseTopBarView_leftIVVisibility, true));
            setLeftCloseShow(typedArray.getBoolean(R.styleable.BaseTopBarView_leftCloseVisibility, false));
            CharSequence leftTVContent = typedArray.getText(R.styleable.BaseTopBarView_leftTVContent);
            setLeftTVContent(leftTVContent == null ? "" : leftTVContent.toString());
            setLeftTVColor(typedArray.getColor(R.styleable.BaseTopBarView_leftTVColor, Color.WHITE));
            setLeftTVFontSize(typedArray.getInteger(R.styleable.BaseTopBarView_leftTVFontSize, 16));
            if (typedArray.getBoolean(R.styleable.BaseTopBarView_leftTVFontBold, false)) {
                setLeftTVContentBold();
            }

            CharSequence title = typedArray.getText(R.styleable.BaseTopBarView_titleContent);
            setTitle(title == null ? "" : title.toString());
            setTitleColor(typedArray.getColor(R.styleable.BaseTopBarView_titleColor, Color.WHITE));
            setTitleFontSize(typedArray.getInteger(R.styleable.BaseTopBarView_titleFontSize, 18));
            if (typedArray.getBoolean(R.styleable.BaseTopBarView_titleFontBold, false)) {
                setTitleContentBold();
            }

            setRightBtnImageShow(typedArray.getBoolean(R.styleable.BaseTopBarView_rightIVVisibility, false));
            setRightBtnImage(typedArray.getResourceId(R.styleable.BaseTopBarView_rightIVResources, 0));
            CharSequence rtv = typedArray.getText(R.styleable.BaseTopBarView_rightTVContent);
            setRightTVContent(rtv == null ? "" : rtv.toString());
            setRightTVColor(typedArray.getColor(R.styleable.BaseTopBarView_rightTVColor, Color.WHITE));
            setRightTVFontSize(typedArray.getInteger(R.styleable.BaseTopBarView_rightTVFontSize, 16));
            if (typedArray.getBoolean(R.styleable.BaseTopBarView_rightTVFontBold, false)) {
                setRightTVContentBold();
            }

            setWhetherShowDividerView(typedArray.getBoolean(R.styleable.BaseTopBarView_dividerVisibility, true));
            typedArray.recycle();
        }
    }

    /**
     * 设置父容器背景颜色
     *
     * @param color 颜色的资源ID
     */
    public void setLayoutBGColor(int color) {
        if (rl_ljtb_layout == null) {
            return;
        }
        rl_ljtb_layout.setBackgroundColor(color);
    }

    /**
     * 设置标题内容
     *
     * @param title 标题文字内容
     */
    public void setTitle(String title) {
        if (tv_ljtb_title == null) {
            return;
        }
        if (TextUtils.isEmpty(title)) {
            tv_ljtb_title.setVisibility(View.GONE);
        } else {
            tv_ljtb_title.setVisibility(View.VISIBLE);
            tv_ljtb_title.setText(title);
        }
    }

    /**
     * 设置标题文字大小
     *
     * @param size 标题文字大小，默认转成sp
     */
    public void setTitleFontSize(int size) {
        if (tv_ljtb_title == null) {
            return;
        }
        if (size > 0) {
            tv_ljtb_title.setTextSize(size);
        }
    }

    /**
     * 设置标题字体颜色
     *
     * @param color 颜色的资源ID
     */
    public void setTitleColor(int color) {
        if (tv_ljtb_title == null) {
            return;
        }
        tv_ljtb_title.setTextColor(color);
    }

    /**
     * 设置标题字体是否加粗
     */
    public void setTitleContentBold() {
        if (tv_ljtb_title == null) {
            return;
        }
        TextPaint paint = tv_ljtb_title.getPaint();
        paint.setFakeBoldText(true);
    }

    /**
     * 设置左侧按钮图标
     *
     * @param res 图片的资源ID
     */
    public void setLeftBtnImage(int res) {
        if (iv_ljtb_left_back == null) {
            return;
        }
        iv_ljtb_left_back.setImageResource(res);
        if (res != 0) {
            setLeftBtnImageShow(true);
        }
    }

    /**
     * 设置左侧按钮图标显示状态
     *
     * @param show 图片控件显示状态
     */
    public void setLeftBtnImageShow(boolean show) {
        if (iv_ljtb_left_back == null) {
            return;
        }
        if (show) {
            iv_ljtb_left_back.setVisibility(View.VISIBLE);
            initViewEvent();
        } else {
            iv_ljtb_left_back.setVisibility(View.GONE);
        }
    }

    /**
     * 设置左侧按钮文本内容
     *
     * @param content 文本文字内容
     */
    public void setLeftTVContent(String content) {
        if (tv_ljtb_left_back == null) {
            return;
        }
        if (TextUtils.isEmpty(content)) {
            tv_ljtb_left_back.setVisibility(View.GONE);
        } else {
            tv_ljtb_left_back.setVisibility(View.VISIBLE);
            tv_ljtb_left_back.setText(content);
            initViewEvent();
        }
    }

    /**
     * 设置左侧按钮文本字体大小
     *
     * @param size 标题文字大小，默认转成sp
     */
    public void setLeftTVFontSize(int size) {
        if (tv_ljtb_left_back == null) {
            return;
        }
        if (size > 0) {
            tv_ljtb_left_back.setTextSize(size);
        }
    }

    /**
     * 设置左侧按钮文本字体颜色
     *
     * @param color 颜色的资源ID
     */
    public void setLeftTVColor(int color) {
        if (tv_ljtb_left_back == null) {
            return;
        }
        tv_ljtb_left_back.setTextColor(color);
    }

    /**
     * 设置左侧侧文本控件字体加粗
     */
    public void setLeftTVContentBold() {
        if (tv_ljtb_left_back == null) {
            return;
        }
        TextPaint paint = tv_ljtb_left_back.getPaint();
        paint.setFakeBoldText(true);
    }

    /**
     * 设置左边 关闭按钮是否显示
     *
     * @param isShow
     */
    public void setLeftCloseShow(boolean isShow) {
        if (rl_ljtb_left_close_layout == null) {
            return;
        }
        if (isShow) {
            rl_ljtb_left_close_layout.setVisibility(View.VISIBLE);
        } else {
            rl_ljtb_left_close_layout.setVisibility(View.GONE);
        }
    }

    /**
     * 设置右侧按钮图标
     *
     * @param res 图片的资源ID
     */
    public void setRightBtnImage(int res) {
        if (iv_ljtb_right_back == null) {
            return;
        }
        iv_ljtb_right_back.setImageResource(res);
        if (res != 0) {
            setRightBtnImageShow(true);
        }
    }

    /**
     * 设置右侧按钮图标显示状态
     *
     * @param show 图片控件显示状态
     */
    public void setRightBtnImageShow(boolean show) {
        if (iv_ljtb_right_back == null) {
            return;
        }
        if (show) {
            iv_ljtb_right_back.setVisibility(View.VISIBLE);
            initViewEvent();
        } else {
            iv_ljtb_right_back.setVisibility(View.GONE);
        }
    }

    /**
     * 设置右侧按钮文本内容
     *
     * @param content 文本文字内容
     */
    public void setRightTVContent(String content) {
        if (tv_ljtb_right_back == null) {
            return;
        }
        if (TextUtils.isEmpty(content)) {
            rl_ljtb_right_layout.setClickable(false);
            tv_ljtb_right_back.setVisibility(View.GONE);
        } else {
            rl_ljtb_right_layout.setClickable(true);
            tv_ljtb_right_back.setVisibility(View.VISIBLE);
            tv_ljtb_right_back.setText(content);
            initViewEvent();
        }
    }

    /**
     * 设置右侧按钮文本字体大小
     *
     * @param size 标题文字大小，默认转成sp
     */
    public void setRightTVFontSize(int size) {
        if (tv_ljtb_right_back == null) {
            return;
        }
        if (size > 0) {
            tv_ljtb_right_back.setTextSize(size);
        }
    }

    /**
     * 设置右侧按钮文本字体颜色
     *
     * @param color 颜色的资源ID
     */
    public void setRightTVColor(int color) {
        if (tv_ljtb_right_back == null) {
            return;
        }
        tv_ljtb_right_back.setTextColor(color);
    }

    /**
     * 设置右侧文本控件字体加粗
     */
    public void setRightTVContentBold() {
        if (tv_ljtb_right_back == null) {
            return;
        }
        TextPaint paint = tv_ljtb_right_back.getPaint();
        paint.setFakeBoldText(true);
    }

    /**
     * 设置右侧按钮文本内容
     */
    public TextView getRightTV() {
        return tv_ljtb_right_back;
    }

    /**
     * 设置左侧按钮文本内容
     */
    public TextView getLeftTV() {
        return tv_ljtb_left_back;
    }

    /**
     * 设置是否显示底部分割线控件
     *
     * @param whetherShow 是否显示
     */
    public void setWhetherShowDividerView(boolean whetherShow) {
        if (view_ljtb_divider == null) {
            return;
        }
        if (whetherShow) {
            view_ljtb_divider.setVisibility(View.VISIBLE);
        } else {
            view_ljtb_divider.setVisibility(View.INVISIBLE);
        }
    }

    /**
     * 获取父容器控件
     */
    public RelativeLayout getRl_ljtb_layout() {
        return rl_ljtb_layout;
    }

    /**
     * 获取布局左侧按钮容器控件
     */
    public LinearLayout getRl_ljtb_left_layout() {
        return rl_ljtb_left_layout;
    }

    /**
     * 获取布局左侧图标控件
     */
    public ImageView getIv_ljtb_left_back() {
        return iv_ljtb_left_back;
    }

    /**
     * 获取布局左侧文本控件
     */
    public TextView getTv_ljtb_left_back() {
        return tv_ljtb_left_back;
    }

    /**
     * 获取布局中间标题文本控件
     */
    public TextView getTv_ljtb_title() {
        return tv_ljtb_title;
    }

    /**
     * 获取布局右侧按钮容器控件
     */
    public LinearLayout getRl_ljtb_right_layout() {
        return rl_ljtb_right_layout;
    }

    /**
     * 获取布局右侧图标控件
     */
    public ImageView getIv_ljtb_right_back() {
        return iv_ljtb_right_back;
    }

    /**
     * 获取布局右侧文本控件
     */
    public TextView getTv_ljtb_right_back() {
        return tv_ljtb_right_back;
    }

    /**
     * 获取布局底部分割线控件
     */
    public View getView_ljtb_divider() {
        return view_ljtb_divider;
    }

    public void setEventInterface(EventInterface event) {
        this.eventInterface = event;
        initViewEvent();
    }

    /**
     * 布局控件事件接口
     */
    public interface EventInterface {

        /**
         * 左侧按钮点击事件
         */
        void leftOnClick();

        /**
         * 左侧关闭点击事件
         */
        void leftCloseClick();

        /**
         * 右侧按钮点击事件
         */
        void rightOnClick();

    }
}
